package com.qingjiang.projectschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingjiang.projectschedule.mapper.StatisticChildMapper;
import com.qingjiang.projectschedule.pojo.StatisticChild;
import com.qingjiang.projectschedule.service.StatisticChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StatisticChildServiceImpl extends ServiceImpl<StatisticChildMapper, StatisticChild> implements StatisticChildService {
    @Autowired
    private StatisticChildMapper statisticChildMapper;

    @Override
    public void insertOrUpdate(Integer deptId, String curYearMonth, int size, Double point) {
        LambdaQueryWrapper<StatisticChild> wrapper = new LambdaQueryWrapper<StatisticChild>();
        wrapper.eq(StatisticChild::getDeptId, deptId).eq(StatisticChild::getAccessTime, curYearMonth);
        StatisticChild statisticChild = statisticChildMapper.selectOne(wrapper);
        if (statisticChild == null) {
            statisticChild = new StatisticChild();
            statisticChild.setDeptId(deptId);
            statisticChild.setAccessTime(curYearMonth);
            statisticChild.setProjectSum(size);
            statisticChild.setPoint(point);
            statisticChildMapper.insert(statisticChild);
        } else {
            LambdaUpdateWrapper<StatisticChild> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(StatisticChild::getProjectSum, size)
                    .set(StatisticChild::getPoint, point)
                    .eq(StatisticChild::getDeptId, deptId)
                    .eq(StatisticChild::getAccessTime, curYearMonth);
            statisticChildMapper.update(null, updateWrapper);
        }
    }

    @Override
    public StatisticChild getHistoryInfoByYearAndMonth(Integer deptId, String curYearMonth) {
        StatisticChild statisticChild = statisticChildMapper.selectOne(new LambdaQueryWrapper<StatisticChild>().eq(StatisticChild::getDeptId, deptId).eq(StatisticChild::getAccessTime, curYearMonth));
        return statisticChild;
    }
}
